window.$config = {
    server : 'http://10.66.42.148:8080',
    on_server : 'campaign.dongfeng-nissan.com.cn',
    interface : true, //接口运行环境，本地，线上
    host: function () {
        if(location.hostname == this.on_server || $config.interface){
            return '/2018/chinanba/enroll/web'
        }else{
            return this.server;
        }
    },
    //校验
    rex : {
        //手机号
        mobile : /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/
    },
    getCookie :function (c_name) {
        if (document.cookie.length>0) {
            var c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1)
            {
                c_start=c_start + c_name.length+1
                var c_end=document.cookie.indexOf(";",c_start)
                if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return ""
    },
    //设置cookie
    setCookie: function (c_name, value, expiredays ) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + value + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())+';path=/';
    },
    GetQueryString:function(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    },
    dataURLtoBlob: function (base64Data) {
        var byteString;
        if (base64Data.split(',')[0].indexOf('base64') >= 0)
            byteString = atob(base64Data.split(',')[1]);
        else
            byteString = unescape(base64Data.split(',')[1]);
        var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];
        var ia = new Uint8Array(byteString.length);
        for (var i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }
        return new Blob([ia], {type:mimeString});
    },
    audioAutoPlay: function(){
        var audio = document.getElementById('bgmusic');
        audio.play();
        document.addEventListener("WeixinJSBridgeReady", function () {
            audio.play();
        }, false);
        document.addEventListener('YixinJSBridgeReady', function() {
            audio.play();
        }, false);
    
        $('.music').click(function(){
            if(audio!==null) {
                if(audio.paused){
                    audio.play();
                    $('.music').addClass('on');
                }else{
                    audio.pause();
                    $('.music').removeClass('on');
                }
            }
        });
    },
    loadScript (src, callback) {
        var script = document.createElement('script'),
            head = document.getElementsByTagName('head')[0];
        script.type = 'text/javascript';
        script.charset = 'UTF-8';
        script.src = src;
        if (script.addEventListener) {
            script.addEventListener('load', function () {
                callback();
            }, false);
        } else if (script.attachEvent) {
            script.attachEvent('onreadystatechange', function () {
                var target = window.event.srcElement;
                if (target.readyState == 'loaded') {
                    callback();
                }
            });
        }
        head.appendChild(script);
    },
    // 滚动页面到指定位置
    toScrollElement (ele){
        if(ele){
            setTimeout(()=>{
                var offTop = document.querySelector(ele).offsetTop;
                document.documentElement.scrollTop = document.body.scrollTop = offTop;
            },100)
        }
    },
    IsPC : function () {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone",
            "SymbianOS", "Windows Phone",
            "iPad", "iPod"];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    },
    iswx : function(){
        var ua = window.navigator.userAgent.toLowerCase();
        if(ua.match(/MicroMessenger/i) == 'micromessenger'){
            return true;
        }else{
            return false;
        }
    },
    wxApi : '',
    wxConfig: function (config) {
        wx.config({
            debug: false,
            appId: config.appId,
            timestamp: config.timestamp,
            nonceStr: config.nonceStr,
            signature: config.signature,
            jsApiList: [
                'chooseImage', 'uploadImage', 'previewImage' ,'onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareWeibo'
            ]
        });

    },
    wxShare : function(Option) {
        if(!this.iswx()) return;
        let _this = this,
            initWxShare = (wxConfig) => {
                _this.wxConfig(wxConfig);
                //初始化
                if (!Option) Option = {};
                var title = Option.title ? Option.title : "第九届品质论坛即将盛大开幕",
                    summary = Option.cont ? Option.cont : "东风日产&东风启辰第9届品质论坛邀请函",
                    link = Option.url ? Option.url : location.href,
                    thumb = Option.logo ? Option.logo : 'http://' + _this.on_server + __uri('/web/img/icon-share.jpg'),
                    content = {
                        title: title,
                        desc: summary,
                        link: link,
                        imgUrl: thumb,
                        /*type: '',
                        dataUrl: '',*/
                        success: function () {
                            if (Option.success) {
                                Option.success();
                            }
                        },
                        cancel: function () {
                        }
                    }
                wx.ready(function () {
                    wx.onMenuShareTimeline(content);
                    wx.onMenuShareAppMessage(content);
                    wx.onMenuShareQQ(content);
                    wx.onMenuShareWeibo(content);
                    // alert(JSON.stringify(content))
                });
            }

        //如果有拿到过key，就直接执行微信sdk
        if(_this.wxApi){
            initWxShare(_this.wxApi)
        }else{
            _this.loadScript('http://res.wx.qq.com/open/js/jweixin-1.2.0.js',function () {
                var hostUrl = '';
                if(location.hostname == _this.on_server){
                    hostUrl = 'http://weixin.dongfeng-nissan.com.cn'
                }
                $.ajax({
                    url : hostUrl+'/lyapi.php?a=CommonApi&m=jssdk&key=0AE6BFEBDEBFC9D955F450954AEAD8E3&t='+new Date().getTime(),
                    data : {
                        url : location.href
                    },
                    dataType : 'jsonp',
                    success : function (json) {
                        _this.wxApi = json;
                        initWxShare(_this.wxApi);
                    }
                })
            })
        }
    },
    // 微信授权
    wxVerify (value){
        if (!this.getCookie('yxq_wx_token') || this.getCookie('yxq_wx_token')=='abc1024' && (location.hostname == 'h5.test.yixiaoqun.com' || location.hostname == 'h5.yixiaoqun.com')) {
            var href = encodeURIComponent(location.href.split('?')[0]+'?aid='+$config.GetQueryString('aid')+(value?value:'')),
                tourl = this.url + '&h52go=' + href;
            location.href = tourl;
            return false;
        }else{
            if(document.body.style.display = 'none'){
                document.body.style.display = 'block';
            }
            return true;
        }
    },
    // 初始化
    init : function () {}
}

// $config.init();

//拓展Vue
if(typeof Vue == 'function'){
    Vue.mixin({
        mounted(){

        },
        methods:{
            common (){
                return {
                    httplock : true,
                    // 转化空格
                    converBr(value) {
                        var str = value.replace(/\r\n/g, '<br>');
                        str = str.replace(/[\r,\n]/g, '<br>');
                        return str;
                    },
                    hosturl (){
                        var base  = '/2018/chinanba/enroll/web' ;
                        if(location.hostname == $config.on_server){
                            return '//'+location.hostname+base
                        }else if ($config.interface){
                            return 'http://campaign.dongfeng-nissan.com.cn'+base
                        }else{
                            return 'http://10.66.42.148:8080'
                        }
                    },
                    layer_alert(value){
                        layer.alert(value)
                    },
                    //返回上一页
                    comeBack (){
                        var length  = history.length;
                        if(length<=1){
                            if($config.IsPC()){
                                location.href = 'index.html'
                            }else{
                                location.href = 'index-m.html'
                            }

                        }else{
                            history.back()
                        }
                    }
                }
            }
        }
    })
}